<template>
  <div class="goods-contanier">
    <!-- 购物车小球 -->
    <transition v-on:before-enter="beforeEnter" v-on:enter="enter" v-on:after-enter="afterEnter">
      <div class="ball" v-if="ballFlag" ref="ball"></div>
    </transition>
    <!-- 轮播图页面 -->
    <lunbotu :lunbotulist="lunbotu, isfull" class="lunbotu"></lunbotu>
    <!-- 购物页面 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsInfo.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>
            原价：
            <del>￥{{ goodsInfo.market_price }}</del> &nbsp;&nbsp; 现价：
            <span class="now_price">￥{{ goodsInfo.sell_price }}</span>
          </p>
          <p>购买数量：
            <number-box
              class="numberbox"
              @getcount="getSelectedCount"
              :max="goodsInfo.stock_quantity"
            ></number-box>
          </p>
          <mt-button type="primary" size="small">立即购买</mt-button>
          <mt-button type="danger" size="small" @click="addShoppingCar">加入购物车</mt-button>
        </div>
      </div>
    </div>
    <!-- 商品详情页面 -->
    <div class="mui-card">
      <div class="mui-card-header">商品详情</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品编码：{{ goodsInfo.goods_no }}</p>
          <p>商品库存：{{ goodsInfo.stock_quantity }}件</p>
          <p>上架时间：{{ goodsInfo.add_time | dataFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文详情</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">评价详情</mt-button>
      </div>
    </div>
  </div>
</template>



<script>
// 引入mint-ui 插件- -
import { Toast } from "mint-ui";
// 引入轮播图组件
import lunbotu from "../../compoents/subcompoents/lunbotu.vue";
// 引入numberBox组件
import numberBox from "../../compoents/subcompoents/numberBox.vue";

export default {
  // 给组件传值----
  data: function() {
    return {
      id: this.$route.params.id, // 存储url获取过来的id
      lunbotu: [],
      isfull: false,
      goodsInfo: {},
      ballFlag: false,
      selectCount: 1
    };
  },

  created() {
    this.getList();
    this.getGoodsInfo();
  },
  methods: {
    getList() {
      this.$http.get("api/getthumimages/" + this.id).then(function(result) {
        if (result.body.status == 0) {
          result.body.message.forEach(item => {
            item.img = item.src;
          });
          this.lunbotu = result.body.message;
          console.log(this.lunbotu);
        }
      });
    },
    getGoodsInfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status == 0) {
          console.log(result.body.message[0]);
          this.goodsInfo = result.body.message[0];
        } else {
          Toast("获取详情失败！");
        }
      });
    },
    goDesc(id) {
      // 跳转到商品详情页面
      this.$router.push({
        name: "goodsDesc",
        params: { id }
      });
    },
    goComment(id) {
      // 跳转到商品评论页面----
      this.$router.push({
        name: "goodsComment"
        // params: { id }
      });
    },
    addShoppingCar() {
      this.ballFlag = !this.ballFlag;
      // 拼接一个对象---将这个商品的数据保存到对象中存到vuex中托管----
      var goodsInfo = {
        id: this.id,
        count: this.selectCount,
        price: this.goodsInfo.sell_price,
        selected: true
      };
      //调用vuex 中mutation 中的方法
      this.$store.commit("addToCar", goodsInfo);
    },
    beforeEnter: function(el) {
      // ...
      el.style.transform = "translate(0, 0)";
    },
    // 当与 CSS 结合使用时
    // 回调函数 done 是可选的
    enter: function(el, done) {
      var boxPosition = this.$refs.ball.getBoundingClientRect();
      var badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      var x = badgePosition.left - boxPosition.left;
      var y = badgePosition.top - boxPosition.top;

      el.offsetWidth;
      el.style.transition = "all 0.5s cubic-bezier(.17,.67,.83,.67)";
      el.style.transform = `translate(${x}px,${y}px)`;
      done();
    },
    afterEnter: function(el) {
      // ...
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(num) {
      this.selectCount = num;
      console.log("子组件传递过来的值" + this.selectCount);
    }
  },
  components: {
    lunbotu: lunbotu,
    numberBox: numberBox
  }
};
</script>

<style lang="scss" scoped>
.goods-contanier {
  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 300px;
    left: 153px;
  }
  .lunbotu {
    text-align: center;
  }
  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
  .mui-card-footer,
  .mui-card-header {
    display: block;
    button {
      margin: 15px 0;
    }
  }
}
</style>
